package com.xjrsoft.module.customerTwo.AppManage.sales_order.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.entity.SalesOrder;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.dto.SalesOrderListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;

import com.xjrsoft.module.customerTwo.AppManage.sales_order.mapper.SalesOrderMapper;
import com.xjrsoft.module.customerTwo.AppManage.sales_order.service.ISalesOrderService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.ITBarinUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *  服务实现类
 *
 * @author lisen
 * @since 2024-04-25
 */
@Service
@AllArgsConstructor
public class SalesOrderServiceImpl extends BaseService<SalesOrderMapper, SalesOrder> implements ISalesOrderService {

	@Resource
	ITBarinUserService userService;

	@Override
	public IPage<SalesOrder> getPageList(SalesOrderListDto pageListDto) {
		Wrapper<SalesOrder> wrapper = Wrappers.<SalesOrder>query().lambda()
				.eq(!StringUtil.isEmpty(pageListDto.getBusiness_opportunity_id()), SalesOrder::getBusinessOpportunityId, pageListDto.getBusiness_opportunity_id())
				.like(!StringUtil.isEmpty(pageListDto.getBusiness_opportunity_number()), SalesOrder::getBusinessOpportunityNumber, pageListDto.getBusiness_opportunity_number())
				.like(!StringUtil.isEmpty(pageListDto.getAssembly_id()), SalesOrder::getAssemblyId, pageListDto.getAssembly_id())
				.like(!StringUtil.isEmpty(pageListDto.getOrder_number()), SalesOrder::getOrderNumber, pageListDto.getOrder_number())
				.eq(!StringUtil.isEmpty(pageListDto.getCustomer_id()), SalesOrder::getCustomerId, pageListDto.getCustomer_id())
				.like(!StringUtil.isEmpty(pageListDto.getCustomer_code()), SalesOrder::getCustomerCode, pageListDto.getCustomer_code())
				.eq(!StringUtil.isEmpty(pageListDto.getContactor_id()), SalesOrder::getContactorId, pageListDto.getContactor_id())
				.like(!StringUtil.isEmpty(pageListDto.getContactor_name()), SalesOrder::getContactorName, pageListDto.getContactor_name())
				.like(!StringUtil.isEmpty(pageListDto.getContactor_phone()), SalesOrder::getContactorPhone, pageListDto.getContactor_phone())
				.ge(!StringUtil.isEmpty(pageListDto.getCreate_date_Start()), SalesOrder::getCreateDate, pageListDto.getCreate_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getCreate_date_End()), SalesOrder::getCreateDate, pageListDto.getCreate_date_End())
				.eq(!StringUtil.isEmpty(pageListDto.getSales_department_id()), SalesOrder::getSalesDepartmentId, pageListDto.getSales_department_id())
				.eq(!StringUtil.isEmpty(pageListDto.getSales_man_id()), SalesOrder::getSalesManId, pageListDto.getSales_man_id())
				.like(!StringUtil.isEmpty(pageListDto.getContract_number()), SalesOrder::getContractNumber, pageListDto.getContract_number())
				.like(!StringUtil.isEmpty(pageListDto.getContract_name()), SalesOrder::getContractName, pageListDto.getContract_name())
				.ge(!StringUtil.isEmpty(pageListDto.getContract_start_date_Start()), SalesOrder::getContractStartDate, pageListDto.getContract_start_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getContract_start_date_End()), SalesOrder::getContractStartDate, pageListDto.getContract_start_date_End())
				.ge(!StringUtil.isEmpty(pageListDto.getContract_end_date_Start()), SalesOrder::getContractEndDate, pageListDto.getContract_end_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getContract_end_date_End()), SalesOrder::getContractEndDate, pageListDto.getContract_end_date_End())
				.eq(!StringUtil.isEmpty(pageListDto.getTax_amount()), SalesOrder::getTaxAmount, pageListDto.getTax_amount())
				.eq(!StringUtil.isEmpty(pageListDto.getNone_tax_amount()), SalesOrder::getNoneTaxAmount, pageListDto.getNone_tax_amount())
				.like(!StringUtil.isEmpty(pageListDto.getContract_instruction()), SalesOrder::getContractInstruction, pageListDto.getContract_instruction())
				.eq(!StringUtil.isEmpty(pageListDto.getSales_assembly_id()), SalesOrder::getSalesAssemblyId, pageListDto.getSales_assembly_id())
				.like(!StringUtil.isEmpty(pageListDto.getState()), SalesOrder::getState, pageListDto.getState())
				.eq(!StringUtil.isEmpty(pageListDto.getAuditor_id()), SalesOrder::getAuditorId, pageListDto.getAuditor_id())
				.ge(!StringUtil.isEmpty(pageListDto.getAudit_date_Start()), SalesOrder::getAuditDate, pageListDto.getAudit_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getAudit_date_End()), SalesOrder::getAuditDate, pageListDto.getAudit_date_End())
				.like(!StringUtil.isEmpty(pageListDto.getReject_reason()), SalesOrder::getRejectReason, pageListDto.getReject_reason())
				.like(!StringUtil.isEmpty(pageListDto.getGeneral_contractor()), SalesOrder::getGeneralContractor, pageListDto.getGeneral_contractor());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public Integer addSalesOrder(SalesOrder salesOrder) {
		/*List<TBarinUser> tBarinUsers = userService.getBaseMapper().selectList(null);
		for(TBarinUser user:tBarinUsers){
			if(salesOrder.getCustomerId().equals(user.getFFirmname())){
				boolean isSuccess = this.save(salesOrder);
				return isSuccess ? salesOrder.getId() : null;
			}
		}*/
		if(userService.getBaseMapper().selectList(null).stream()
				.anyMatch(user -> salesOrder.getCustomerId().equals(user.getFFirmname()))){
			boolean isSuccess = this.save(salesOrder);
			return isSuccess ? salesOrder.getId() : null;
		}
		return null;
	}

	@Override
	public boolean updateSalesOrder(String id, SalesOrder salesOrder) {
		salesOrder.setId(Integer.valueOf(id));
		return this.updateById(salesOrder);
	}

	@Override
	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		return this.removeByIds(idList);
	}

}